package cn.itliuliu.org.controller;

import cn.itliuliu.basic.util.AjaxResult;
import cn.itliuliu.basic.util.PageList;
import cn.itliuliu.org.domain.Sdydetail;
import cn.itliuliu.org.query.SdydetailQuery;
import cn.itliuliu.org.service.ISdydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RequestMapping("/sdyde")
@RestController
@Api(tags = "部门接口")
@Validated
public class SdydetailController {
    @Autowired
    private ISdydetailService sdydetailService;

    @GetMapping("/{id}")
    @ApiOperation("通过id查询部门")
    public Sdydetail queryOne(@PathVariable("id") Long id) {
        return sdydetailService.queryOne(id);
    }

    @GetMapping
    public List<Sdydetail> queryAll() {
        return sdydetailService.queryAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody @Valid Sdydetail sdydetail) {
        if (sdydetail.getId() != null) {
            sdydetailService.update(sdydetail);
        } else {
            sdydetailService.add(sdydetail);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        sdydetailService.delete(id);
        return AjaxResult.me();
    }

    @PostMapping
    public PageList<Sdydetail> queryPage(@RequestBody SdydetailQuery sdydetailQuery) {
        return sdydetailService.queryPage(sdydetailQuery);
    }

    /**
     * 这里既可以用数组接收也可以用List接收
     * 用什么类型来接收对sql有影响
     *
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        sdydetailService.patchDelete(ids);
        return AjaxResult.me();
    }

    @GetMapping("/tree")
    public List<Sdydetail> queryTree() {
        return sdydetailService.queryTree();
    }
}
